/*订单预览页*/
var Pay = {};

Pay.init = function(id){
	
	//扩展Date的format方法   
    Date.prototype.format = function (format) {  
        var o = {  
            "M+": this.getMonth() + 1,  
            "d+": this.getDate(),  
            "h+": this.getHours(),  
            "m+": this.getMinutes(),  
            "s+": this.getSeconds(),  
            "q+": Math.floor((this.getMonth() + 3) / 3),  
            "S": this.getMilliseconds()  
        }  
        if (/(y+)/.test(format)) {  
            format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));  
        }  
        for (var k in o) {  
            if (new RegExp("(" + k + ")").test(format)) {  
                format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));  
            }  
        }  
        return format;  
    }  
	
	
	Pay.initBtn(id);
	Pay.initMsg(id);

	$(".pay-type-title").attr("href","/index.html#/order_add?id="+id);
}
//初始化按钮
Pay.initBtn = function(id){
	$('.pay-type-tab').click(function(){
		var index = $(this).index()+1;
		$('.pay-type-tab').removeClass('active');
		$(this).addClass('active');
		$('.pay-tab').hide();
		$('.pay-type-tab'+index).show();
	});
	
	  
	$('.pay-fs-check').click(function(){
		$('.pay-fs-check').removeClass('active');
		$(this).addClass('active');
	});
	
	$('.btn-pay').click(function(){
		var way;
		way=$(".pay-fs-check.active").attr("name");
		if($(".yinlian").hasClass("active")){
			way=2;
			var names="";
			var paths="";
			$(".upload-file-list .upload-file-name").each(function(){
				names+=$(this).text()+",";
				paths+=$(this).attr("data")+",";
			});
			names=(names.substring(names.length-1)==',')?names.substring(0,names.length-1):names;
			paths=(paths.substring(paths.length-1)==',')?paths.substring(0,paths.length-1):paths;
			if(isNull(paths)) {
				loboxWarningAlert('请上传转账凭证');
				return;
			}
			
			//保存文件
			var param = {
					url:'shop/file',
					ajaxType:'POST',
					name:names,
					type:10,
					objectId:id,
					path:paths
					
			}
			
			doInvoke(param,function(data){
				succFunAlert("操作成功，我们会尽快核实账单！",function(){
					window.location.href='/#/order_list';
				});
			});
		}else {
			//支付
			var param = {
				url:'shop/account',
				ajaxType:'POST',
				code:$("#orderCode").val(),
				end: "1",
				way: way,
				
			}
			
			doInvoke(param,function(data){
				window.location.href=data.data;
			});
		}
		
		
		
		
		
		
//		$("#payDialog").modal("show");
//		$(".btn-success").click(function(){
//			$("#payDialog").modal("hide");
//			window.location.href='#/pay_success';
//		});
	});
	
	
	
};
//初始化数据
Pay.initMsg = function(id){

	var param = {
			url:'shop/order/'+id+'/order',
			ajaxType:'PUT',
	}
	
	doInvoke(param,function(data){
	    if(data.code == '8002'){
	      var orderCode=data.data;
	      
	      var param = {
	  			url:'shop/account/'+orderCode,
	  			ajaxType:'GET',
	  	}
	      doInvoke(param,function(data){
	    	  if(data.code=="10000"){
	    		  var orderdata=data.data;
	    		  
	    	       var pattern = "yyyy-MM-dd hh:mm:ss";  
	    	       orderdata.mTime=new Date(orderdata.deliverTime*1000).format(pattern); 
	    		 
	    		  
	    		  switch(orderdata.packageType){
	    		  case "0":orderdata.packType="BGA"; break;
	    		  case "1":orderdata.packType="LGA"; break;
	    		  case "2":orderdata.packType="QFN"; break;
	    		  case "3":orderdata.packType="QFP"; break;
	    		  case "4":orderdata.packType="其他封装形式"; break;
	    		  }
	    		  if(!orderdata.original){
	    			  orderdata.original="0";
	    		  }
	    		  $("#payCount").text("￥"+(orderdata.money));
	    		  
	    		  $(".pay_orderInfo").empty().append($("#payorderInfo-Template").render(orderdata));
	    		  $("#orderCode").val(orderdata.code);
	    		  if(orderdata.discountRate==0){
	    			  $(".discount").hide();
	    			  $(".originalFee").hide();
	    			  
	    		  }
	    	  }
	    	  
	      });
	      
	    }else{
	      errorAlert(data.message);
	    }
		});

};

Pay.editOrder=function(){
	window.location.href = "/index.html#/order_add?id="+$("#orderId").val();
}
